<?php
/**
 *@author iflamed@126.com
 *@todo Operate the channel of each category
 */
class ChannelAction extends BaseAction{
	/**
	 * @todo Show all channel of all the category
	 */
	public function index(){
		$Channel = M('Channel');
		$Category = M('Category');
		if(isset($_GET['category']))
			$allchannel = $Channel->where('channel_category='.$_GET['category'])->select();
		else
			$allchannel = $Channel->select();
		/**
		 * @param $value will print out
		 */
		$value=array();
		foreach ($allchannel as $key=>$Channel){
			$acategory = $Category -> where('category_id='.$Channel['channel_category']) -> find();
			//array_push($Channel,$acategory['category_name']);
			$Channel['channel_category_name']=$acategory['category_name'];
			$value[]=$Channel;
		}
		$this->assign('channels',$value);
		$this->display();
	}
	/**
	 * @todo To add a new channel
	 */
	public function addchannel(){
		$Channel = M('Channel');
		$Channel->create();
		$isadd = $Channel->add();
		if($isadd){
			$this->assign('waitSecond',5);
			$this->assign('jumpUrl','index');
			$this->success('Succeed to add a new Channel!');
		}else {
			$this->assign('waitSecond',5);
			$this->assign('jumpUrl','index');
			$this->error('Failed to add a new Channel!');
		}
	}
	/**
	 * @todo Update a channel
	 */
	public function updatechannel(){
		$Channel= M('Channel');
		$Channel -> create();
		$isupdate = $Channel->where('channel_id='.$_POST['channel_id'])->save();
		if($isupdate){
			$this->assign('waitSecond',5);
			$this->assign('jumpUrl','index');
			$this->success('Succeed to update a new channel!');
		}else {
			$this->assign('waitSecond',5);
			$this->assign('jumpUrl','index');
			$this->error('Failed to update a new channel!');
		}
	}
	/**
	 * @todo Delete a channel
	 */
	public function deletechannel(){
		$Channel = M('Channel');
		$Content = M('Content');
		$isnull = $Content->where('channel_id='.$_GET['id'])->select();
		if(count($isnull) == 0)
			$isdelete = $Channel->where('channel_id='.$_GET['id'])->delete();
		else 	
			$isdelete = FALSE;
		if($isdelete){
			$this->assign('waitSecond',5);
			$this->assign('jumpUrl','index');
			$this->success('Succeed to delete a Channel!');
		}else {
			$this->assign('waitSecond',5);
			$this->assign('jumpUrl','index');
			$this->error('Failed to delete a Channel! You should delete all content of this channel');
		}
	}
	/**
	 * @todo to show the page to fill info of a channel
	 */
	public function newchannel(){
		$Category = M('Category');
		$categories = $Category->select();
		$this->assign('categories',$categories);
		$this->display();
	}
	/**
	 * @todo to print the pate for editing a channel
	 */
	public function editchannel(){
		$Channel = M('Channel');
		$Category = M('Category');
		$achannel = $Channel->where('channel_id='.$_GET['id'])->find();
		/**
		 * @param $value will print out
		 */
		$acategory = $Category -> where('category_id='.$achannel['channel_category']) -> find();
		$achannel['channel_category_name']=$acategory['category_name'];
		$categories = $Category->select();
		$this->assign('categories',$categories);
		$this->assign('channel',$achannel);
		$this->display();
	}
}
?>